package com.wuzhishan.news.service.Impl;

import com.wuzhishan.news.mapper.articleMapper;
import com.wuzhishan.news.mapper.commentMapper;
import com.wuzhishan.news.mapper.notifyMapper;
import com.wuzhishan.news.pojo.*;
import com.wuzhishan.news.service.commentService;
import com.wuzhishan.news.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class commentServiceImpl implements commentService {
    @Autowired
    private articleMapper articlemapper;
    @Autowired
    private commentMapper commentMapper;
    @Autowired
    private notifyMapper notifyMapper;

    /**
     * 添加文章评论
     * @param toId
     * @param pid
     * @param text
     * @param session
     * @return
     */
    @Override
    public Result addComment(String toId, String pid, String text, HttpSession session) {
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if (user == null){
            //前台根据返回的0，判断为当前用户未登录
            return Result.of(0);
        }
        try {
            BlogComment comment = new BlogComment();
            BlogArticle article = articlemapper.getArticleById(toId);
            comment.setArticle(article);
            comment.setUser(user);
            comment.setContent(text);
            comment.setCreated(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            if(!"0".equals(pid)){
                BlogComment com = commentMapper.getCommentById(pid);
                System.out.println(com);
                if (com == null){
                    return Result.of(3,"对不起，该用户已不存在!");
                }
                comment.setComment(com);
            }
            //插入评论
            commentMapper.addComment(comment);

            //给文章作者发通知
            BlogNotify notify = new BlogNotify();
            notify.setAvatar(user.getAvatar());
            String content = String.format("评论了你的文章 - <a href=\"/view/%s.html\">点击查看详情",article.getId());
            notify.setContent(content);
            notify.setCreated(DateUtil.now());
            notify.setTargetId(String.valueOf(article.getUser().getId()));
            notify.setTitle(user.getNickname());
            notify.setUser(user);
            notifyMapper.saveNotify(notify);
            return Result.of(1,"评论成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.of(2,"评论失败，请稍后再试!");
        }
    }

    /**
     * 获取当前文章评论(分页实现)
     * @param articleId
     * @param pS
     * @param cP
     * @return
     */
    @Override
    public Result selectPageComment(String articleId, String pS, String cP) {
        Long pageSize = Long.parseLong(pS);
        Long currentPage = Long.parseLong(cP);
        Long pageIndex = (currentPage - 1) * pageSize;
        Pageable pageable = new PageRequest((int) (currentPage - 1), 6);
        List<BlogComment> list = commentMapper.selectPageComment(articleId, pageIndex, pageSize);
        Page<BlogComment> data = new PageImpl(list,pageable,list.size());
        return Result.of(1,"操作成功",data);

        /*Long pageSize = Long.parseLong(pS);
        Long currentPage = Long.parseLong(cP);
        Long pageIndex = (currentPage - 1) * pageSize;
        List<BlogComment> data = commentMapper.selectPageComment(articleId,pageIndex,pageSize);
        return Result.of(1,"操作成功",data);*/
    }
}
